package m3_publish_subscribe;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class Producer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.64.140");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");

        Channel channel = connectionFactory.newConnection().createChannel();

        //定义 fanout类型 交换机 ,扇出   :   logs
        //channel.exchangeDeclare("logs", "fanout"); ← 两种写法 ↓
        channel.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);

        //向交换机发送消息
        while (true){
            System.out.println("输入消息:");
            String msg = new Scanner(System.in).nextLine();
            channel.basicPublish(
                    "logs",         //向哪一个交换机发送消息
                    "",             //向交换机发送数据时,不向队列发送消息,写了也无效
                    null,
                    msg.getBytes());
        }
    }
}
